package electsys;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author t
 * Created 2010-7-1  16:44:37
 * Last Modified 2010-7-1  16:44:37
 *
 */
public class MyUtil {

    public static String getStringFromStream(InputStream in, String charset) {
        try {
            String ret = "";
            BufferedReader br = new BufferedReader(new InputStreamReader(in, charset));
            String tmp = "";
            while ((tmp = br.readLine()) != null) {
                ret += tmp;
            }
            br.close();
            return ret;
        } catch (IOException ex) {
            Logger.getLogger(MyUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static String getURLByPrefix(String in, String prefix) {
        String ret = "";
        int index = in.indexOf(prefix);
        if (index > 0) {
            ret = in.substring(index, in.indexOf("\"", index));
            return ret;
        } else {
            return null;
        }
    }

    public static String getSubStr(String str, String pre, String suff) {
        String tmp = "";
        int index = str.indexOf(pre);
        if (index >= 0 && index < str.length()) {
            index += pre.length();
            int end = str.indexOf(suff, index);
            if (end < 0 || end >= str.length()) {
                return tmp;
            }
            tmp = str.substring(index, end);
        }
        return tmp;
    }

    public static String getVIEWSTATE(String getpage) {
        String tmp = getpage.substring(getpage.indexOf(MyConst.PAGE_VIEWSTATE_START) + MyConst.PAGE_VIEWSTATE_START.length(), getpage.indexOf(MyConst.PAGE_VIEWSTATE_END, getpage.indexOf(MyConst.PAGE_VIEWSTATE_START)));
        return tmp;
    }

    public static String getEVENTVALIDATION(String getpage) {
        String tmp = getpage.substring(getpage.indexOf(MyConst.PAGE_EVENTVALIDATION_START) + MyConst.PAGE_EVENTVALIDATION_START.length(), getpage.indexOf(MyConst.PAGE_EVENTVALIDATION_end, getpage.indexOf(MyConst.PAGE_EVENTVALIDATION_START)));
        return tmp;
    }

    public static String getLTBXNOptions(String getpage) {
        return getSubStr(getpage, "<select name=\"dpXn\" id=\"dpXn\">", "</select>");
    }

    public static String getLTBXQOptions(String getpage) {
        return getSubStr(getpage, "<select name=\"dpXq\" id=\"dpXq\">", "</select>");
    }

    public static String getTitle(String getpage) {
        //String tmp = getpage.substring(getpage.indexOf("<title>")+7,getpage.indexOf("</title>",getpage.indexOf("<title>")));
        //return tmp;
        return getSubStr(getpage, "<title>", "</title>");
    }

    public static String getNameFromForm(String page) {
        //String tmp = page.substring(page.indexOf("name=\"")+6,page.indexOf("\"",page.indexOf("name=\"")+6));
        //return tmp;
        return getSubStr(page, "name=\"", "\"");
    }

    public static String getValueFromForm(String page) {
        //String tmp = page.substring(page.indexOf("value=\"")+7,page.indexOf("\"",page.indexOf("value=\"")+7));
        //return tmp;
        return getSubStr(page, "value=\"", "\"");
    }

    public static String getOptionFromForm(String page) {
        return getSubStr(page, "<option", "</option>");
    }

    public static List<String> getOptionsFromForm(String page) {
        List<String> ops = new ArrayList();
        String select = getSubStr(page, "<select name=\"dpXn\" id=\"dpXn\">", "</select>");
        String ret = getOptionFromForm(select);
        while (!ret.equals("")) {
            ops.add(getValueFromForm(ret));
            select = select.substring(select.indexOf("</option>") + 9);
            if (select.indexOf("<option") < 0) {
                return ops;
            }
            ret = getOptionFromForm(select);
        }
        return ops;
    }

    public static String getSelectedOption(String page) {
        //int start = page.indexOf(MyConst.OPTION_START+" "+MyConst.SELECTED_START)+MyConst.OPTION_START.length()+MyConst.SELECTED_START.length()+2;
        //String tmp = page.substring(start,page.indexOf(MyConst.OPTION_END,start));
        //return tmp;
        return getSubStr(page, "<option selected=\"selected\"", "</option>");
    }

    public static String getLessonSpan(String page) {
        return getSubStr(page, "<span id=\"LessonTbl1_spanContent\">", "</span>");
    }

    public static String getLessonTableName(String page) {
        return getSubStr(page, "<span id=\"Label1\">", "</span>");
    }

    public static String getLessonTable(String page) {
        return getSubStr(page, "<table width='100%' border='1' cellspacing='0' cellpadding='1' align='center' class='alltab'>", "</table>");
    }

    public static String getExamTable(String page) {
        return getSubStr(page, "<table cellspacing=\"1\" rules=\"all\" border=\"0\" id=\"gridMain\" width=\"100%\">", "</table>");
    }

    public static List<String> getTRs(String page) {
        List<String> trs = new ArrayList();
        String ret = getSubStr(page, "<tr", "</tr>");
        while (!ret.equals("")) {
            //System.out.println(ret);
            //System.out.println(page.length());
            trs.add(ret);
            page = page.substring(page.indexOf("</tr>") + 5);
            if (page.indexOf("<tr") < 0) {
                break;
            }
            ret = getSubStr(page, "<tr", "</tr>");
        }
        return trs;
    }

    public static List<String> getTDs(String tr) {
        List<String> tds = new ArrayList();
        String ret = tr.substring(tr.indexOf("<td"), tr.indexOf("</td>") + 5);
        while (!ret.equals("")) {
            tds.add(ret);
            tr = tr.substring(tr.indexOf("</td>") + 5);
            if (tr.indexOf("<td") < 0) {
                break;
            }
            ret = tr.substring(tr.indexOf("<td"), tr.indexOf("</td>") + 5);
        }
        return tds;
    }

    public static int getTDStartNum(String tr) {
        return Integer.parseInt(getSubStr(tr, "<td align='center' class='classtit2'>", "</td>").trim());
    }

    public static int getTDLength(String td) {
        String l = getSubStr(td, "<td align='center' class='classmain' rowspan=", ">").trim();
        if (l.equals("")) {
            return 1;
        }
        return Integer.parseInt(l);
    }

    public static String getLessonID(String info) {
        return getSubStr(info, "bsid=", "\'");
    }

    public static List<String> getLessonInfos(String td) {
        List<String> infos = new ArrayList();
        String info = getSubStr(td, "<a href='../../lesson/", "<br>");
        while (!info.equals("")) {
            info = info + "<br>";
            infos.add(info);
            td = td.substring(td.indexOf("<br>") + 4);
            info = getSubStr(td, "<a href='../../lesson/", "<br>");
        }
        //System.out.println(info);
        return infos;
    }

    public static String getLessonName(String info) {
        //System.out.println(info);
        //System.out.println(info);
        //System.out.println(info.lastIndexOf("（"));
        
        String ret = info.substring(info.indexOf("target=_blank>") + 14, info.lastIndexOf("（"));
        if ( ret.indexOf("<") >= 0 && ret.indexOf(">") >=0 ) ret = ret.substring(ret.indexOf(">")+1);
        return ret;
    }

    public static List<Integer> getLessonWeek(String info) {
        List<Integer> week = new ArrayList();
        //System.out.println(info);
        String w = getSubStr(info, "</a>", "<br>");
        //System.out.println(w);
        int start = Integer.parseInt(info.substring(info.lastIndexOf("（") + 1, info.indexOf("-", info.lastIndexOf("（") + 1)).trim());
        int end = Integer.parseInt(info.substring(info.indexOf("-", info.lastIndexOf("（")) + 1, info.lastIndexOf("）") - 1).trim());
        for (int i = start; i <= end; i++) {
            if ((w.equals("") || !w.equals("双周")) && i % 2 == 1) {
                week.add(Integer.valueOf(i));
            }
            if ((w.equals("") || !w.equals("单周")) && i % 2 == 0) {
                week.add(Integer.valueOf(i));
            }
        }
        return week;
    }

    public static String getLessonWeekStr(String info) {
        String w = getSubStr(info, "</a>", "<br>");
        if (!w.equals("单周") && !w.equals("双周")) {
            w = "";
        }
        return info.substring(info.lastIndexOf("（") + 1, info.lastIndexOf("）")) + w;
    }

    public static String getLessonLocation(String info) {
        return getSubStr(info, "[", "]");
    }

    public static String getLessonStartTime(String tr) {
        return MyConst.TIMEOFSTART[getTDStartNum(tr) - 1];
    }

    public static String getLessonEndTime(String tr, String td) {
        return MyConst.TIMEOFEND[getTDStartNum(tr) + getTDLength(td) - 2];
    }

    public static String getValueFromTD(String td) {
        return getSubStr(td, ">", "</td>");
    }

    public static String getExamStartTime(String tdv) {
        String date = tdv.substring(0, 10);
        int ind = tdv.lastIndexOf("－");
        //String st1 = tdv.substring(ind - 5, ind - 3);
        String st1 = tdv.substring(tdv.indexOf("星期") + 3, ind - 3);
        String st2 = tdv.substring(ind - 2, ind);
        //if (!st1.substring(0,1).equals("1") && !st1.substring(0,1).equals("2")) st1.replaceFirst(st1.substring(0, 1), "0");
        if (st1.length() == 1) {
            st1 = "0" + st1;
        }
        if (!st2.substring(0, 1).equals("1") && !st2.substring(0, 1).equals("2")) {
            st2.replaceFirst(st2.substring(0, 1), "0");
        }
        //System.out.println(st1);
        //System.out.println(st2);
        String ret = date + "T" + st1 + ":" + st2 + ":00";
        //System.out.println(ret);
        return ret;
    }

    public static String getExamEndTime(String tdv) {
        String date = tdv.substring(0, 10);
        int ind = tdv.lastIndexOf("－");
        String st1 = tdv.substring(ind + 1, ind + 3);
        String st2 = tdv.substring(ind + 4, ind + 6);
        if (!st1.substring(0, 1).equals("1") && !st1.substring(0, 1).equals("2")) {
            st1.replaceFirst(st1.substring(0, 1), "0");
        }
        if (!st2.substring(0, 1).equals("1") && !st2.substring(0, 1).equals("2")) {
            st2.replaceFirst(st2.substring(0, 1), "0");
        }
        String ret = date + "T" + st1 + ":" + st2 + ":00";
        return ret;
    }

    public static int getDurationMin(String start, String end) {
        //Calendar cal1 = Calendar.getInstance();
        //Calendar cal2 = Calendar.getInstance();
        //cal1.set(Calendar.HOUR_OF_DAY, Integer.parseInt(start.substring(0, 2)));
        //cal1.set(Calendar.MINUTE, Integer.parseInt(start.substring(3,5)));
        //cal2.set(Calendar.HOUR_OF_DAY, Integer.parseInt(end.substring(0, 2)));
        //cal2.set(Calendar.MINUTE, Integer.parseInt(end.substring(3, 5)));
        int h1 = Integer.parseInt(start.substring(0, 2));
        int m1 = Integer.parseInt(start.substring(3,5));
        int h2 = Integer.parseInt(end.substring(0, 2));
        int m2 = Integer.parseInt(end.substring(3, 5));
        if (h2 < h1 || (h2==h1&&m2<m1)) return 0;
        return (h2-h1) * 60 + m2 - m1;
    }
}
